﻿using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// MySpaceView.xaml 的交互逻辑
    /// </summary>
    public partial class MySpaceView : HuiZeWindow
    {
        public MySpaceViewModel mySpaceViewModel { get; set; }

        public ReqGroupFileListDetialModel[] SelectedItems
        {
            get
            {
                if (_list_selected_id != null && _list_selected_id.Count > 0 && _dic_my_space_item != null)
                {
                    var _list = new List<ReqGroupFileListDetialModel>();
                    foreach (var _id in _list_selected_id)
                    {
                        if (_dic_my_space_item.ContainsKey(_id))
                        {
                            _list.Add(_dic_my_space_item[_id]);
                        }
                    }
                    return _list.ToArray();
                }
                return null;
            }
        }

        public MySpaceView()
        {
            InitializeComponent();
            this.MaxHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height + 10;
            this.MaxWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width + 10;
            Loaded += LoginView_Loaded;
        }

        public void Updata(int pages)
        {
            //MySpaceViewModel ResourceViewModel = DataContext as MySpaceViewModel;
            //var list = mySpaceViewModel.spaceFileList(pages);
            //if (list != null)
            //{
            //    myImageList.Children.Clear();
            //    foreach (var item in list)
            //    {
            //        mySpaceEdit book = new mySpaceEdit();
            //        book.NomalImage = item.file;
            //        book.Labtext = item.file_name;
            //        book.Id = item.id.ToString();
            //        book.Margin = new Thickness(10, 10, 0, 0);
            //        book.Height = 200;
            //        book.Width = 200;
            //        myImageList.Children.Add(book);
            //        book.MouseDown += Book_Click;
            //    }
            //}
        }

        private void Book_Click(object sender, RoutedEventArgs e)
        {
            //MySpaceViewModel ResourceViewModel = DataContext as MySpaceViewModel;
            mySpaceEdit bookbtn = sender as mySpaceEdit;
            if (!bookbtn.IsSelectd)
            {
                mySpaceViewModel.SeletedResCount++;
            }
            else
            {
                mySpaceViewModel.SeletedResCount--;
            }
            //string content = chk.Content.ToString();
            //planModel.RemoveSetattachment_ids(0, content);
        }

        //private void PageManage_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        //{
        //    if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
        //    {
        //        CustomRadioButton button = e.OriginalSource as CustomRadioButton;
        //        string index = button.Content.ToString();
        //        if (index == ">")
        //        {
        //            //index = pageManage.Content.ToString();
        //            index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
        //        }
        //        if (index == "<")
        //        {
        //            index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
        //        }
        //        if (index == "首页")
        //        {
        //            index = "1";
        //        }
        //        if (index == "尾页")
        //        {
        //            index = pageManage.ItemSourceCount.ToString();
        //        }
        //        Updata(Convert.ToInt32(index));
        //    }
        //}

        private void LoginView_Loaded(object sender, RoutedEventArgs e)
        {
            //ImgGrid.Width = this.Width - 300;
            //ImgGrid.Height = this.Height - 200;
            _dic_my_space_item = new Dictionary<int, ReqGroupFileListDetialModel>();
            MySpaceViewModel ResourceViewModel = DataContext as MySpaceViewModel;
            mySpaceViewModel = ResourceViewModel;
            ResourceViewModel.mySpaceView = this;
            pageManage.LoadDataCallback = Load_MySpaceItem;

            Load_MySpaceItem(1);
            mySpaceViewModel.GetGroupList(1);
            pageManage.ItemSourceCount = mySpaceViewModel.pageCount;
            pageManage.UpdataPages();
            if (CommonMethods.MySpaceSelectedId == null)
                CommonMethods.MySpaceSelectedId = new List<string>();
            mySpaceViewModel.SelectedGroups = mySpaceViewModel.MyGroups_Filter[0];

            //Updata(1);
            //pageManage.ItemSourceCount = ResourceViewModel.pageCount;
            //if (CommonMethods.MySpaceSelectedId != null)
            //    CommonMethods.MySpaceSelectedId.Clear();

            //if (ResourceViewModel.SeletedResCount != null)
            //    ResourceViewModel.SeletedResCount = 0;

            //var list = ResourceViewModel.spaceFileList();
            //if (list != null)
            //{
            //    myImageList.Children.Clear();
            //    foreach (var item in list)
            //    {
            //        //StackPanel stack = new StackPanel
            //        //{

            //        //    Width = 150,
            //        //    Height = 180,
            //        //    HorizontalAlignment = HorizontalAlignment.Left,
            //        //    Margin = new Thickness(20,0,0,0),

            //        //};
            //        //Image image = new Image
            //        //{
            //        //    Source = new BitmapImage(new Uri(item.file)),
            //        //    //Name= item.name,
            //        //    Width = 150,
            //        //    Height = 160

            //        //};
            //        //Label lb = new Label
            //        //{
            //        //    Content = item.file_name,
            //        //    HorizontalAlignment = HorizontalAlignment.Center
            //        //};
            //        //stack.Children.Add(image);
            //        //stack.Children.Add(lb);

            //        BookEdit2 book = new BookEdit2();
            //        book.NomalImage = item.file;
            //        book.Labtext = item.file_name;
            //        book.Margin = new Thickness(10, 10, 0, 0);
            //        book.Height = 200;
            //        book.Width = 200;
            //        myImageList.Children.Add(book);
            //    }
            //}
        }

        private void close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            Close();
        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void YCButton_Click_1(object sender, RoutedEventArgs e)
        {
            //if (_list_selected_id != null && _list_selected_id.Count > 0)
            //    CommonMethods.MySpaceSelectedId.AddRange(_list_selected_id.Select(x => x.ToString()));

            this.DialogResult = true;
            Close();
        }

        void YCWateTextBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Load_MySpaceItem(1);
        }

        void YCWateTextBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource is YuanCode.Control.Controls.YCButton)
                Load_MySpaceItem(1);
        }

        void YCWateTextBox_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

        }

        private void YCWateTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            //if (e.OriginalSource.ToString().Contains("搜索"))
            //{
            //    Updata(1);
            //}
        }

        void Load_MySpaceItem(int _page_index)
        {
            _list_my_space_item = mySpaceViewModel.spaceFileList(_page_index);
            PART_MySpaceItems.ItemsSource = _list_my_space_item;
            if (_list_my_space_item != null && _list_my_space_item.Count > 0)
            {
                foreach (var _item in _list_my_space_item)
                {
                    if (!_dic_my_space_item.ContainsKey(_item.id))
                        _dic_my_space_item.Add(_item.id, null);
                    _dic_my_space_item[_item.id] = _item;
                }
            }
        }

        void PART_MySpace_Item_Loaded(object sender, RoutedEventArgs e)
        {
            PART_MySpace_Item _item = (PART_MySpace_Item)sender;
            _item.EditCallback = EditMySpaceItem;
            _item.DeleteCallback = DeleteMySpaceItem;
            _item.SelectedCallback = SelectFile;

            _item.SetIsSelected(CommonMethods.MySpaceSelectedId.Contains(_item.Data.id.ToString()));
        }

        void EditMySpaceItem(ReqGroupFileListDetialModel _data)
        {
            EditFileGroupView _win = new EditFileGroupView(_data.id, _data.group_id);
            _win.DataContext = mySpaceViewModel;
            _win.Owner = this;
            if (_win.ShowDialog() == true)
            {
                Refresh();
            }
        }

        void DeleteMySpaceItem(ReqGroupFileListDetialModel _data)
        {
            if (mySpaceViewModel.ShowDialogView_Ethan("此操作将永久删除，是否继续？"))
            {
                if (mySpaceViewModel.delFile(_data.id))
                {
                    Refresh();
                }
            }
        }

        void SelectFile(int _id, bool _is_select)
        {
            if (_list_selected_id == null)
                _list_selected_id = new HashSet<int>();

            if (_is_select)
                _list_selected_id.Add(_id);
            else
                _list_selected_id.Remove(_id);

            if (mySpaceViewModel != null)
                mySpaceViewModel.SeletedResCount = _list_selected_id.Count;
        }

        public void Refresh()
        {
            Load_MySpaceItem(1);
        }



        HashSet<int> _list_selected_id;
        List<ReqGroupFileListDetialModel> _list_my_space_item;
        Dictionary<int, ReqGroupFileListDetialModel> _dic_my_space_item;
    }
}
